package com.youfang.sixthmedia.calc;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.youfang.sixthmedia.R;
import com.youfang.sixthmedia.calc.YoufangCalc.DEBJ;
import com.youfang.sixthmedia.calc.YoufangCalc.DEBX;

public class CalcResultActivity extends Activity {

	Adapter adatper;
	ListView listview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.calc_result_activity);
		adatper = new Adapter(this);
		listview = (ListView) this.findViewById(R.id.listview);
		listview.setAdapter(adatper);
		Intent intent = this.getIntent();
		String type = intent.getStringExtra("type");
		if ("debj".equals(type)) {
			((TextView) this.findViewById(R.id.title)).setText("等额本金计算结果");

			this.findViewById(R.id.debj).setVisibility(View.VISIBLE);
			this.findViewById(R.id.debx).setVisibility(View.INVISIBLE);
			DEBJ debj = (DEBJ) intent.getSerializableExtra("data");
			setupDEBJ(debj);

		} else {
			((TextView) this.findViewById(R.id.title)).setText("等额本息计算结果");
			this.findViewById(R.id.debj).setVisibility(View.INVISIBLE);
			this.findViewById(R.id.debx).setVisibility(View.VISIBLE);
			DEBX debx = (DEBX) intent.getSerializableExtra("data");
			setupDEBX(debx);
		}

	}

	public static String format(double data) {

		return String.format("%.2f", data);

	}

	public void setupDEBX(DEBX debx) {

		((TextView) this.findViewById(R.id.loanCount))
				.setText(format(debx.loanCount) + "万元");
		((TextView) this.findViewById(R.id.paymentCount))
				.setText(format(debx.paymentCount) + "万元");
		((TextView) this.findViewById(R.id.loanInterest))
				.setText(format(debx.loanInterest) + "万元");

		((TextView) this.findViewById(R.id.averagePayment))
				.setText(format(debx.averagePayment) + "元");
		((TextView) this.findViewById(R.id.monthTotal)).setText(debx.monthTotal
				+ "月");

	}

	public void setupDEBJ(DEBJ debj) {
		((TextView) this.findViewById(R.id.debj_loanCount))
				.setText(format(debj.loanCount) + "万元");

		((TextView) this.findViewById(R.id.debj_paymentCount))
				.setText(format(debj.paymentCount) + "万元");

		((TextView) this.findViewById(R.id.debj_loanInterest))
				.setText(format(debj.loanInterest) + "万元");

		((TextView) this.findViewById(R.id.debj_monthTotal))
				.setText(debj.monthTotal + "月");

		// paylist...
		adatper.addAll(debj.payList);
		adatper.notifyDataSetChanged();

	}

	static class Adapter extends com.gzit.adapter.CommonListAdapter<String> {

		public Adapter(Context context) {
			super(context);
			this.setItemLayout(R.layout.calc_item);

		}

		@Override
		protected void render(String item, View view, int position) {
			TextView label = (TextView) view.findViewById(R.id.label);
			label.setText("第" + (position + 1) + "月还款额：");
			TextView amount = (TextView) view.findViewById(R.id.amount);
			amount.setText(format(Double.parseDouble(item)) + "元");

		}

	}

}
